#include "OptionsState.h"
#include "../Game.h"
#include "MainMenuState.h"
#include "../SGD Wrappers/SGD_String.h"
using std::ostringstream;

COptionsState* COptionsState::GetInstance()
{
	static COptionsState s_Instance;
	return &s_Instance;
}

COptionsState::COptionsState(void)
{
	m_pD3D	= nullptr;
	m_pTM	= nullptr;
	m_pDI	= nullptr;
	m_pXA	= nullptr;
//IMAGES
	m_nBackGroundImgID		= -1;
//SOUNDS
	m_nBackGroundSfxID		= -1;
	m_nBackGroundMusicID	= -1;
	m_nCursorMove			= 1;
	m_nScreenWidth			= CGame::GetInstance()->GetWidth();
	m_nScreenHeight			= CGame::GetInstance()->GetHeight();
	m_sMusicVol				= 0;
	m_sSfxVol				= 0;
}

COptionsState::~COptionsState(void) { }

void COptionsState::Enter(void)
{
	this->m_pD3D	= CSGD_Direct3D::GetInstance();
	this->m_pTM		= CSGD_TextureManager::GetInstance();
	this->m_pDI		= CSGD_DirectInput::GetInstance();
	this->m_pXA		= CSGD_XAudio2::GetInstance();
//IMAGES
	//this->m_nBackGroundImgID = m_pTM->LoadTexture(_T("resource/graphics/..LOAD BACK GROUND IMAGE HERE.."), D3DCOLOR_XRGB(0, 0, 0) );
//SOUNDS
	this->m_nBackGroundSfxID = m_pXA->SFXLoadSound(_T("resources/sounds/CJM_SFXBackGround.wav"));
	this->m_nBackGroundMusicID = m_pXA->MusicLoadSong(_T("resources/sounds/CJM_BackGroundMusic.xwm"));
	
	if (!m_pXA->MusicIsSongPlaying(m_nBackGroundMusicID))
	{
		m_pXA->MusicStopSong( m_nBackGroundMusicID );
		m_pXA->MusicPlaySong(m_nBackGroundMusicID, true);
	}

	m_pFont = new CBitmapFont();
	m_pFont->Load("resources/XML/DeadlyShadowsFont.xml");

	LoadVolume("resources/sounds/volume.txt");
}

void COptionsState::Exit(void)
{
//IMAGE
	//if( m_nBackGroundImgID != -1 )
	//{
	//	m_pTM->UnloadTexture( m_nBackGroundImgID );
	//	m_nBackGroundImgID = -1;
	//}
//SOUND
	if( m_nBackGroundMusicID != -1 )
	{
		m_pXA->MusicStopSong( m_nBackGroundMusicID );
		m_pXA->MusicUnloadSong( m_nBackGroundMusicID );
		m_nBackGroundMusicID = -1;
	}
	if( m_nBackGroundSfxID != -1 )
	{
		m_pXA->SFXStopSound( m_nBackGroundSfxID );
		m_pXA->SFXUnloadSound( m_nBackGroundSfxID );
		m_nBackGroundSfxID = -1;
	}
//FONTS
	if (m_pFont != nullptr)
	{
		delete m_pFont;
		m_pFont = nullptr;
	}
	SaveVolume("resources/sounds/volume.txt" );
}

bool COptionsState::Input(void)
{
	if (m_pDI->KeyPressed(DIK_ESCAPE))
	{
		CGame::GetInstance()->AddState(CMainMenuState::GetInstance());
		CGame::GetInstance()->RemoveState(this);
		return true;
	}
//CURSOR MOVEMENT------------------------
	if (m_pDI->KeyPressed(DIK_UP ))
	{
		if (m_nCursorMove == 0)
		{
			m_nCursorMove = 2; 
		}
		m_nCursorMove -=1;
	}
	if (m_pDI->KeyPressed(DIK_DOWN))
	{
		if (m_nCursorMove == 1)
		{
			m_nCursorMove = -1;
		}
		m_nCursorMove += 1;
	}
//VOLUME CONTROL FOR BACKGROUND MUSIC--------------------------------
	if (m_nCursorMove == 0)
	{
		if (!m_pXA->MusicIsSongPlaying(m_nBackGroundMusicID))
		{
			m_pXA->MusicStopSong( m_nBackGroundMusicID );
			m_pXA->MusicPlaySong(m_nBackGroundMusicID, true);
		}
		if (m_pDI->KeyPressed(DIK_RIGHT))
		{
			if (m_sMusicVol != 100)
				m_sMusicVol += 5;
			if (m_sMusicVol == 100)
				m_sMusicVol = 100;
		}
		if (m_pDI->KeyPressed(DIK_LEFT ))
		{
			if (m_sMusicVol != 0)
				m_sMusicVol-=5;
			if (m_sMusicVol == 0)
				m_sMusicVol = 0;
		}
		m_pXA->MusicSetMasterVolume(m_sMusicVol * 0.01f);
	}
//VOLUME CONTROL FOR SOUND EFFECTS----------------------------------
	if (m_nCursorMove == 1)
	{
		m_pXA->MusicStopSong( m_nBackGroundSfxID );

		if (m_pDI->KeyPressed(DIK_RIGHT))
		{
			if (this->m_pXA->SFXIsSoundPlaying( m_nBackGroundSfxID ) == false )
			{
				this->m_pXA->SFXPlaySound( m_nBackGroundSfxID );
			}
			if (m_sSfxVol != 100)
				m_sSfxVol += 5;
			if (m_sSfxVol == 100)
				m_sSfxVol = 100;
		}
		if (m_pDI->KeyPressed(DIK_LEFT ))
		{
			if (this->m_pXA->SFXIsSoundPlaying( m_nBackGroundSfxID ) == false )
			{
				this->m_pXA->SFXPlaySound( m_nBackGroundSfxID );
			}
			if (m_sSfxVol != 0)
				m_sSfxVol-=5;
			if (m_sSfxVol == 0)
				m_sSfxVol = 0;
		}
		m_pXA->SFXSetMasterVolume(m_sSfxVol * 0.01f);
	}

	return true;
}

void COptionsState::Update(float fElapsedTime)
{
	m_pXA->Update();
}

void COptionsState::Render(void)
{
	//m_pTM->Draw(m_nBackGroundImgID, (int)(m_nScreenWidth * 0.5f) - m_nScreenWidth, ((int)(m_nScreenHeight * 0.5f) - m_nScreenHeight) + 130, 1.0f, 1.0f, nullptr, 0, 0, 0); 
	//m_pD3D->GetSprite()->Flush();

//Return to main menu-----------------------------
	this->m_pFont->Print("options", (int)m_nScreenWidth * 0.2f, (int)m_nScreenHeight * 0.05f, 1.0f, D3DXCOLOR(255,0,0,255));
	this->m_pFont->Print("(esc) return to main menu", (int)(m_nScreenWidth * 0.5) - (strlen("(esc) return to main menu") * 8), (int)m_nScreenHeight * 0.95f, 1.0f, D3DXCOLOR(255,0,0,255));

//Layout of the Text for Master / Sfx-------------
	if (m_nCursorMove == 1)
	{
		this->m_pFont->Print("music Volume", (int)(m_nScreenWidth * 0.2f), (int)(m_nScreenHeight * 0.15f), 0.75f, D3DXCOLOR(255,0,0,255));
		this->m_pFont->Print("SFX Volume", (int)(m_nScreenWidth * 0.2f), (int)(m_nScreenHeight * 0.25f), 0.75f, D3DXCOLOR(255,255,255,255));
	}
	if (m_nCursorMove == 0)
	{
		this->m_pFont->Print("music Volume", (int)(m_nScreenWidth * 0.2f), (int)(m_nScreenHeight * 0.15f), 0.75f, D3DXCOLOR(255,255,255,255));
		this->m_pFont->Print("SFX Volume", (int)(m_nScreenWidth * 0.2f), (int)(m_nScreenHeight * 0.25f), 0.75f, D3DXCOLOR(255,0,0,255));
	}
//MASTER VOLUME=======================================================================================================================================
	if (m_sMusicVol == 0)
		this->m_pFont->Print("0", (int)(m_nScreenWidth * 0.50f), (int)(m_nScreenHeight * 0.15f), 0.75f, D3DXCOLOR(0,255,0,255));
	if (m_sMusicVol == 5)
		this->m_pFont->Print("5", (int)(m_nScreenWidth * 0.50f), (int)(m_nScreenHeight * 0.15f), 0.75f, D3DXCOLOR(0,255,0,255));
	if (m_sMusicVol == 10)
		this->m_pFont->Print("10", (int)(m_nScreenWidth * 0.50f), (int)(m_nScreenHeight * 0.15f), 0.75f, D3DXCOLOR(0,255,0,255));
	if (m_sMusicVol == 15)
		this->m_pFont->Print("15", (int)(m_nScreenWidth * 0.50f), (int)(m_nScreenHeight * 0.15f), 0.75f, D3DXCOLOR(0,255,0,255));
	if (m_sMusicVol == 20)
		this->m_pFont->Print("20", (int)(m_nScreenWidth * 0.50f), (int)(m_nScreenHeight * 0.15f), 0.75f, D3DXCOLOR(0,255,0,255));
	if (m_sMusicVol == 25)										 
		this->m_pFont->Print("25", (int)(m_nScreenWidth * 0.50f), (int)(m_nScreenHeight * 0.15f), 0.75f, D3DXCOLOR(0,255,0,255));
	if (m_sMusicVol == 30)										 
		this->m_pFont->Print("30", (int)(m_nScreenWidth * 0.50f), (int)(m_nScreenHeight * 0.15f), 0.75f, D3DXCOLOR(0,255,0,255));
	if (m_sMusicVol == 35)										
		this->m_pFont->Print("35", (int)(m_nScreenWidth * 0.50f), (int)(m_nScreenHeight * 0.15f), 0.75f, D3DXCOLOR(0,255,0,255));
	if (m_sMusicVol == 40)										 
		this->m_pFont->Print("40", (int)(m_nScreenWidth * 0.50f), (int)(m_nScreenHeight * 0.15f), 0.75f, D3DXCOLOR(0,255,0,255));
	if (m_sMusicVol == 45)										 
		this->m_pFont->Print("45", (int)(m_nScreenWidth * 0.50f), (int)(m_nScreenHeight * 0.15f), 0.75f, D3DXCOLOR(0,255,0,255));
	if (m_sMusicVol == 50)										 
		this->m_pFont->Print("50", (int)(m_nScreenWidth * 0.50f), (int)(m_nScreenHeight * 0.15f), 0.75f, D3DXCOLOR(0,255,0,255));
	if (m_sMusicVol == 55)										 
		this->m_pFont->Print("55", (int)(m_nScreenWidth * 0.50f), (int)(m_nScreenHeight * 0.15f), 0.75f, D3DXCOLOR(0,255,0,255));
	if (m_sMusicVol == 60)
		this->m_pFont->Print("60", (int)(m_nScreenWidth * 0.50f), (int)(m_nScreenHeight * 0.15f), 0.75f, D3DXCOLOR(0,255,0,255));
	if (m_sMusicVol == 65)
		this->m_pFont->Print("65", (int)(m_nScreenWidth * 0.50f), (int)(m_nScreenHeight * 0.15f), 0.75f, D3DXCOLOR(0,255,0,255));
	if (m_sMusicVol == 70)
		this->m_pFont->Print("70", (int)(m_nScreenWidth * 0.50f), (int)(m_nScreenHeight * 0.15f), 0.75f, D3DXCOLOR(0,255,0,255));
	if (m_sMusicVol == 75)
		this->m_pFont->Print("75", (int)(m_nScreenWidth * 0.50f), (int)(m_nScreenHeight * 0.15f), 0.75f, D3DXCOLOR(0,255,0,255));
	if (m_sMusicVol == 80)
		this->m_pFont->Print("80", (int)(m_nScreenWidth * 0.50f), (int)(m_nScreenHeight * 0.15f), 0.75f, D3DXCOLOR(0,255,0,255));
	if (m_sMusicVol == 85)
		this->m_pFont->Print("85", (int)(m_nScreenWidth * 0.50f), (int)(m_nScreenHeight * 0.15f), 0.75f, D3DXCOLOR(0,255,0,255));
	if (m_sMusicVol == 90)
		this->m_pFont->Print("90", (int)(m_nScreenWidth * 0.50f), (int)(m_nScreenHeight * 0.15f), 0.75f, D3DXCOLOR(0,255,0,255));
	if (m_sMusicVol == 95)
		this->m_pFont->Print("95", (int)(m_nScreenWidth * 0.50f), (int)(m_nScreenHeight * 0.15f), 0.75f, D3DXCOLOR(0,255,0,255));
	if (m_sMusicVol == 100)
		this->m_pFont->Print("100", (int)(m_nScreenWidth * 0.50f), (int)(m_nScreenHeight * 0.15f), 0.75f, D3DXCOLOR(0,255,0,255));

//SFX VOLUME=======================================================================================================================================	
	if (m_sSfxVol == 0)
		this->m_pFont->Print("0", (int)(m_nScreenWidth * 0.50f), (int)(m_nScreenHeight * 0.25f), 0.75f, D3DXCOLOR(0,255,0,255));
	if (m_sSfxVol == 5)
		this->m_pFont->Print("5", (int)(m_nScreenWidth * 0.50f), (int)(m_nScreenHeight * 0.25f), 0.75f, D3DXCOLOR(0,255,0,255));
	if (m_sSfxVol == 10)
		this->m_pFont->Print("10", (int)(m_nScreenWidth * 0.50f), (int)(m_nScreenHeight * 0.25f), 0.75f, D3DXCOLOR(0,255,0,255));
	if (m_sSfxVol == 15)
		this->m_pFont->Print("15", (int)(m_nScreenWidth * 0.50f), (int)(m_nScreenHeight * 0.25f), 0.75f, D3DXCOLOR(0,255,0,255));
	if (m_sSfxVol == 20)
		this->m_pFont->Print("20", (int)(m_nScreenWidth * 0.50f), (int)(m_nScreenHeight * 0.25f), 0.75f, D3DXCOLOR(0,255,0,255));
	if (m_sSfxVol == 25)
		this->m_pFont->Print("25", (int)(m_nScreenWidth * 0.50f), (int)(m_nScreenHeight * 0.25f), 0.75f, D3DXCOLOR(0,255,0,255));
	if (m_sSfxVol == 30)
		this->m_pFont->Print("30", (int)(m_nScreenWidth * 0.50f), (int)(m_nScreenHeight * 0.25f), 0.75f, D3DXCOLOR(0,255,0,255));
	if (m_sSfxVol == 35)
		this->m_pFont->Print("35", (int)(m_nScreenWidth * 0.50f), (int)(m_nScreenHeight * 0.25f), 0.75f, D3DXCOLOR(0,255,0,255));
	if (m_sSfxVol == 40)
		this->m_pFont->Print("40", (int)(m_nScreenWidth * 0.50f), (int)(m_nScreenHeight * 0.25f), 0.75f, D3DXCOLOR(0,255,0,255));
	if (m_sSfxVol == 45)
		this->m_pFont->Print("45", (int)(m_nScreenWidth * 0.50f), (int)(m_nScreenHeight * 0.25f), 0.75f, D3DXCOLOR(0,255,0,255));
	if (m_sSfxVol == 50)
		this->m_pFont->Print("50", (int)(m_nScreenWidth * 0.50f), (int)(m_nScreenHeight * 0.25f), 0.75f, D3DXCOLOR(0,255,0,255));
	if (m_sSfxVol == 55)
		this->m_pFont->Print("55", (int)(m_nScreenWidth * 0.50f), (int)(m_nScreenHeight * 0.25f), 0.75f, D3DXCOLOR(0,255,0,255));
	if (m_sSfxVol == 60)
		this->m_pFont->Print("60", (int)(m_nScreenWidth * 0.50f), (int)(m_nScreenHeight * 0.25f), 0.75f, D3DXCOLOR(0,255,0,255));
	if (m_sSfxVol == 65)
		this->m_pFont->Print("65", (int)(m_nScreenWidth * 0.50f), (int)(m_nScreenHeight * 0.25f), 0.75f, D3DXCOLOR(0,255,0,255));
	if (m_sSfxVol == 70)
		this->m_pFont->Print("70", (int)(m_nScreenWidth * 0.50f), (int)(m_nScreenHeight * 0.25f), 0.75f, D3DXCOLOR(0,255,0,255));
	if (m_sSfxVol == 75)
		this->m_pFont->Print("75", (int)(m_nScreenWidth * 0.50f), (int)(m_nScreenHeight * 0.25f), 0.75f, D3DXCOLOR(0,255,0,255));
	if (m_sSfxVol == 80)
		this->m_pFont->Print("80", (int)(m_nScreenWidth * 0.50f), (int)(m_nScreenHeight * 0.25f), 0.75f, D3DXCOLOR(0,255,0,255));
	if (m_sSfxVol == 85)
		this->m_pFont->Print("85", (int)(m_nScreenWidth * 0.50f), (int)(m_nScreenHeight * 0.25f), 0.75f, D3DXCOLOR(0,255,0,255));
	if (m_sSfxVol == 90)
		this->m_pFont->Print("90", (int)(m_nScreenWidth * 0.50f), (int)(m_nScreenHeight * 0.25f), 0.75f, D3DXCOLOR(0,255,0,255));
	if (m_sSfxVol == 95)
		this->m_pFont->Print("95", (int)(m_nScreenWidth * 0.50f), (int)(m_nScreenHeight * 0.25f), 0.75f, D3DXCOLOR(0,255,0,255));
	if (m_sSfxVol == 100)
		this->m_pFont->Print("100", (int)(m_nScreenWidth * 0.50f), (int)(m_nScreenHeight * 0.25f), 0.75f, D3DXCOLOR(0,255,0,255));
}

bool COptionsState::LoadVolume(const char* szFileName)
{
	ifstream loadFile (szFileName);
	if (loadFile.is_open())
	{
		int master;
		int sfx;
		loadFile >> master;
		loadFile >> sfx;
		m_sMusicVol = master;
		m_sSfxVol = sfx;
		loadFile.close();
	}
	return true;
}

void COptionsState::SaveVolume(const char* szFileName)
{
	ofstream saveFile(szFileName);
	if (saveFile.is_open())
	{
		if (saveFile.good())
		{
			saveFile << m_sMusicVol;
			saveFile << "\n";
			saveFile << m_sSfxVol;
		}
	}
}